package com.example.lowbird.lowbird.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cjj.MaterialRefreshLayout;
import com.cjj.MaterialRefreshListener;
import com.daimajia.slider.library.Animations.DescriptionAnimation;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.daimajia.slider.library.Tricks.ViewPagerEx;
import com.example.lowbird.lowbird.R;
import com.example.lowbird.lowbird.activity.GoodActivity;
import com.example.lowbird.lowbird.activity.GoodlistActivity;
import com.example.lowbird.lowbird.adapter.Myrvadapter;
import com.example.lowbird.lowbird.app.Myapp;
import com.example.lowbird.lowbird.bean.Recommendbean;
import com.example.lowbird.lowbird.bean.SlideBean;
import com.example.lowbird.lowbird.constant.Contants;
import com.example.lowbird.lowbird.java.RecycleViewDivider;
import com.google.gson.Gson;
//import com.zhy.http.okhttp.OkHttpUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link HostFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link HostFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HostFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private static Context mContext;
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;
    private static SliderLayout mSl_host;


    private RecyclerView mRv_host;
    private Recommendbean bean = new Recommendbean();
    private Myrvadapter mAdapter;
    private MaterialRefreshLayout hostrefresh;

    public HostFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment HostFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static HostFragment newInstance(String param1, String param2) {
        HostFragment fragment = new HostFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getContext();
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_host, container, false);
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

        mRv_host = ((RecyclerView) view.findViewById(R.id.rv_host));
        mAdapter = new Myrvadapter(bean, mContext);
        mAdapter.setCallback(new Myrvadapter.Hostinterface() {
            @Override
            public void gogoods(int id) {


                Intent intent = new Intent(mContext, GoodlistActivity.class);

                intent.putExtra("id", id);
                startActivity(intent);

            }
        });
        mRv_host.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.VERTICAL, 8, Color.WHITE));
        mRv_host.setAdapter(mAdapter);
        mRv_host.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
        initrecomd();
        hostrefresh = ((MaterialRefreshLayout) view.findViewById(R.id.hostrefresh));
        setmrtfragment();


    }

    private void setmrtfragment() {
        hostrefresh.setMaterialRefreshListener(
                new MaterialRefreshListener() {

                    @Override
                    public void onRefresh(final MaterialRefreshLayout materialRefreshLayout) {

                        new Thread() {
                            @Override
                            public void run() {
                                try {


                                    if (mAdapter.getItemCount() <= 1) {

                                        initrecomd();
                                    } else {
                                        Thread.sleep(500);
                                    }
                                    hostrefresh.finishRefresh();
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }.start();
                    }
                });
        hostrefresh.setIsOverLay(false);
        hostrefresh.setWaveShow(true);
        hostrefresh.setWaveColor(Color.RED);


    }

    private void initrecomd() {
        Request request = new Request.Builder().get().url(Contants.API.CAMPAIGN_HOME).build();

        Myapp.okhttpclient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                String string = response.body().string();
                string = "{'data':" + string + "}";
                Log.e("TAG", "onResponse: " + Contants.API.BASE_URL + Contants.API.CAMPAIGN_HOME + "..." + string);
                final Recommendbean recommendbean = new Gson().fromJson(string, Recommendbean.class);
                mRv_host.post(new Runnable() {
                    @Override
                    public void run() {
                        mAdapter.addrecomend(recommendbean);
                    }
                });

            }
        });

    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }


    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}
